package com.android.gardensnake;

import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.ViewFlipper;
import android.widget.TabHost.TabSpec;

public class Tabs extends Activity implements OnClickListener {

	TabHost th;
	TextView showResults1, showResults2, showResults3, showResults4,
			showResults5;
	Button bStart, bStop;
	long start, stop, result;
	int counter = 1;
	Chronometer chrono;
	ViewFlipper flippy;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tabs);
		th = (TabHost) findViewById(R.id.tabhost);
		th.setup();
		bStart = (Button) findViewById(R.id.bStartWatch);
		bStop = (Button) findViewById(R.id.bStopWatch);
		chrono = (Chronometer) findViewById(R.id.chronometer1);
		chrono.setBase(SystemClock.elapsedRealtime());
		showResults1 = (TextView) findViewById(R.id.tvShowResults1);
		showResults2 = (TextView) findViewById(R.id.tvShowResults2);
		showResults3 = (TextView) findViewById(R.id.tvShowResults3);
		showResults4 = (TextView) findViewById(R.id.tvShowResults4);
		showResults5 = (TextView) findViewById(R.id.tvShowResults5);
		bStart.setOnClickListener(this);
		bStop.setOnClickListener(this);
		TabSpec specs = th.newTabSpec("tag1");
		specs.setContent(R.id.tab1);
		specs.setIndicator("StopWatch");
		th.addTab(specs);
		flippy = (ViewFlipper) findViewById(R.id.viewFlipper1);
		flippy.setOnClickListener(this);
		flippy.setFlipInterval(1000);
		flippy.startFlipping();
		specs = th.newTabSpec("tag2");
		specs.setContent(R.id.tab2);
		specs.setIndicator("Tab 2");
		th.addTab(specs);
		Button bAddTab = (Button) findViewById(R.id.bAddTab);
		bAddTab.setOnClickListener(this);
		specs = th.newTabSpec("tag3");
		specs.setContent(R.id.tab3);
		specs.setIndicator("Add a Tab");
		th.addTab(specs);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_tabs, menu);
		return true;
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		flippy.showNext();
		switch (arg0.getId()) {
		case R.id.bAddTab:
			TabSpec newSpec = th.newTabSpec("tag1");
			newSpec.setContent(new TabHost.TabContentFactory() {

				@Override
				public View createTabContent(String tag) {
					// TODO Auto-generated method stub
					TextView text = new TextView(Tabs.this);
					text.setText("New Tab Created!");
					return text;
				}
			});
			newSpec.setIndicator("New");
			th.addTab(newSpec);
			break;
		case R.id.bStartWatch:
			if (bStart.getText().toString().equalsIgnoreCase("start")) {
				start = System.currentTimeMillis();
				chrono.start();
				bStop.setClickable(true);
				bStop.setText("Lap");
				bStart.setText("Stop");
				break;
			} else {
				stop = System.currentTimeMillis()-start;
				result = result+stop;
				chrono.stop();
				bStop.setText("Reset");
				bStart.setText("Start");
			}
			break;
		case R.id.bStopWatch:
			if (bStop.getText().toString().equalsIgnoreCase("lap")) {
				stop = System.currentTimeMillis()-start;
				result = result+stop;
				start = System.currentTimeMillis();
				int millis = (int) result;
				int seconds = (int) (result / 1000);
				result=0;
				int minutes = seconds / 60;
				millis = millis % 1000;
				seconds = seconds % 60;
				switch (counter) {
				case 1:
					showResults1.setText(String.format("%d:%02d:%02d", minutes,
							seconds, millis));
					break;
				case 2:
					showResults2.setText(String.format("%d:%02d:%02d", minutes,
							seconds, millis));
					break;
				case 3:
					showResults3.setText(String.format("%d:%02d:%02d", minutes,
							seconds, millis));
					break;
				case 4:
					showResults4.setText(String.format("%d:%02d:%02d", minutes,
							seconds, millis));
					break;
				case 5:
					showResults5.setText(String.format("%d:%02d:%02d", minutes,
							seconds, millis));
					break;
				}
				counter++;
			} else {
				chrono.setBase(SystemClock.elapsedRealtime());
				showResults1.setText("00:00.000");
				showResults2.setText("00:00.000");
				showResults3.setText("00:00.000");
				showResults4.setText("00:00.000");
				showResults5.setText("00:00.000");
				bStop.setClickable(false);
				counter = 1;
				result=0;
			}
			break;
		}
	}

}
